package com.globalwave.common.service;

import com.globalwave.base.BaseServiceImpl;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.entity.File;
import com.globalwave.common.entity.FileCriterion;

public class FileServiceImpl 
       extends BaseServiceImpl 
       implements FileService {

    public File create(File file) throws Exception {
        return (File) jdbcDao.insert(file, true);
    }

    public void update(File file) throws Exception {
        jdbcDao.update(file) ;
    }

    public void delete(File file) throws Exception {
        
        jdbcDao.delete(file) ;
        
    }

    public void deleteAll(Long[] fileIds) throws Exception {
        FileCriterion criterion = new FileCriterion() ;
        criterion.setFileIds(fileIds) ;
        jdbcDao.delete(File.class, criterion) ;
        
    }

    public ArrayPageList<File> query(
            FileCriterion fileCriterion, 
            int pageIndex, 
            int pageSize, 
            ArrayOrderList orderList) throws Exception {
        
        if (orderList == null) {
            orderList = new ArrayOrderList() ;
            orderList.addAsc("file_id") ;
        }
        
        return (ArrayPageList<File>)jdbcDao.query(fileCriterion, pageIndex, pageSize, orderList, File.class);
    }


    
    public java.io.File get(Long id) throws Exception {
        FileCriterion criterion = new FileCriterion() ;
        criterion.setFileId(id) ;
        
        ArrayPageList<?> result = jdbcDao.query(FileCriterion.SQL_GET_FILE_ONLY, criterion, null, null, null, File.class, true);
        if (result.size() > 0 ) {
            return ((File) result.get(0)).getContent_() ;            
        } else {
            return null ;
        }
    }
    
}
